package com.hotel.controllers;

import com.hotel.entitys.RestPOJO;
import com.hotel.entitys.Role;
import com.hotel.services.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="员工角色管理接口")//tags:说明信息
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(
            value = "查询员工角色",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/role")
    public RestPOJO selAllRole(){
        RestPOJO restPOJO = new RestPOJO();
        List<Role> list = roleService.selAllRole();
        if(!list.isEmpty()){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(list);
        }else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查询单个员工角色通过id",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 获取单个员工的角色
    @GetMapping("/role/distribute/{id}")
    public RestPOJO getStaffRole(@PathVariable("id") Integer staff_id){
        RestPOJO restPOJO = new RestPOJO();
        List<Integer> checkedList = roleService.getCheckedRole(staff_id);
        List<Role> roleList = roleService.selAllRole();
        Map<String,List> roleMap = new HashMap<String, List>();
        roleMap.put("checkedRoles",checkedList);
        roleMap.put("rolesList",roleList);
        restPOJO.setData(roleMap);
        return restPOJO;
    }

    @ApiOperation(
            value = "分配员工角色",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 保存新分配的角色
    @PostMapping("/role")
    public RestPOJO saveRole(@RequestBody Map<String,Object> map){
        RestPOJO restPOJO = new RestPOJO();
        Integer staff_id = (Integer) map.get("staff_id");
        List<Integer> list = (List<Integer>) map.get("checkedRoles");
        roleService.deleteStaffRole(staff_id);
        int count = roleService.updRole(staff_id,list);
        if (count > 0){
            restPOJO.setMessage("分配角色成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("分配角色失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
}
